package com.haitaoit.chengquan.main;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.ExitApplication;
import com.haitaoit.chengquan.module.car.CarFragment;
import com.haitaoit.chengquan.module.car.SeverFragment;
import com.haitaoit.chengquan.module.center.CenterFragment;
import com.haitaoit.chengquan.module.center_service.CenterServiceFragment;
import com.haitaoit.chengquan.module.home.HomeFragment;
import com.haitaoit.chengquan.module.login.LoginActivity;
import com.haitaoit.chengquan.module.near.NearFragment;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class MainFragmentActivity extends FragmentActivity {
    @ViewInject(R.id.content)
    FrameLayout content;
    /**
     * 网络
     */
    @ViewInject(R.id.main_network)
    static  TextView    main_network;
    /**
     * 底部四个按钮
     */
    @ViewInject(R.id.rb_tab_home)
    private RadioButton tab_home;
    @ViewInject(R.id.rb_tab_near)
    private RadioButton tab_near;
    @ViewInject(R.id.rb_tab_car)
    private RadioButton tab_car;
    @ViewInject(R.id.rb_tab_center)
    private RadioButton tab_center;

    private int index = 0;

    //用来记录底部购物车button,进行
    private int shopingTable = 0;
    private int mianTable    = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_main_fragment);
        ViewUtils.inject(this);

        // 注册监听网络状态的服务
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mReceiver, mFilter);

        index = getIntent().getIntExtra("index", 0);
        setChioceItem(index);
        // 服务方
        if ("2".equals(PreferenceUtils.getPrefString("account_type", "")) ||
                "3".equals(PreferenceUtils.getPrefString("account_type", ""))) {
            tab_car.setText("服务订单");
        }
        HomeFragment homeFragment = new HomeFragment();
        NearFragment nearFragment = new NearFragment();
    }

    @OnClick({R.id.rb_tab_home, R.id.rb_tab_near, R.id.rb_tab_car, R.id.rb_tab_center})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_tab_home:
                index = 0;
                break;
            case R.id.rb_tab_near:
                index = 1;
                break;
            case R.id.rb_tab_car:
                index = 2;
                break;
            case R.id.rb_tab_center:
                index = 3;
                break;
            case R.id.main_network:
                // 唤起系统设置页面
                startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
        }
        setChioceItem(index);
    }

    public void setChioceItem(int index) {
        FragmentManager fm = getSupportFragmentManager();
        // 创建一个Fragment事务
        FragmentTransaction transaction = fm.beginTransaction();
        Fragment fragment = null;
        switch (index) {
            case 0:// 首页
                tab_home.setChecked(true);
                fragment = new HomeFragment();
                transaction.addToBackStack(null);
                transaction.replace(R.id.content, fragment);
                break;
            case 1:// 附近
                tab_near.setChecked(true);
                fragment = new NearFragment();
                transaction.addToBackStack(null);
                transaction.replace(R.id.content, fragment);
                break;
            case 2:// 购物车
                if (PreferenceUtils.getPrefString("user_id", "").equals("")) {

                    //一切先跳转到首页
                    tab_home.setChecked(true);
                    fragment = new HomeFragment();
                    transaction.addToBackStack(null);
                    transaction.replace(R.id.content, fragment);


                    Intent intent = new Intent(MainFragmentActivity.this, LoginActivity.class);
                    intent.putExtra("tag", "2");
                    startActivity(intent);


                } else {
                    tab_car.setChecked(true);
                    if ("2".equals(PreferenceUtils.getPrefString("account_type", "")) ||
                            "3".equals(PreferenceUtils.getPrefString("account_type", ""))) {
                        fragment = new SeverFragment();
                        transaction.addToBackStack(null);
                        transaction.replace(R.id.content, fragment);
                    } else {
                        fragment = new CarFragment();
                        //                Bundle bun = new Bundle();
                        //                bun.putInt("index", index_eva);
                        //                fragment.setArguments(bun);
                        transaction.addToBackStack(null);
                        transaction.replace(R.id.content, fragment);
                    }
                }
                break;
            case 3:// 我的
                if (PreferenceUtils.getPrefString("user_id", "").equals("")) {
                    //一切先跳转到首页
                    tab_home.setChecked(true);
                    fragment = new HomeFragment();
                    transaction.addToBackStack(null);
                    transaction.replace(R.id.content, fragment);

                    Intent intent = new Intent(MainFragmentActivity.this, LoginActivity.class);
                    intent.putExtra("tag", "2");
                    startActivity(intent);
                } else {
                    tab_center.setChecked(true);
                    if ("1".equals(PreferenceUtils.getPrefString("account_type", ""))) {
                        fragment = new CenterFragment();
                    } else if ("2".equals(PreferenceUtils.getPrefString("account_type", "")) ||
                            "3".equals(PreferenceUtils.getPrefString("account_type", ""))) {
                        fragment = new CenterServiceFragment();
                    }
                    transaction.addToBackStack(null);
                    transaction.replace(R.id.content, fragment);
                }
                break;
        }
        transaction.commit();
    }

    long a = 2000;// 双击退出间隔数(毫秒)
    long b = 0;
    public static final int CMNET   = 3;
    public static final int CMWAP   = 2;
    public static final int WIFI    = 1;
    public static       int netType = -1;
    public static ConnectivityManager connMgr;
    public static final String ACTION_NAME = "1";

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getAPNType(context);
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && KeyEvent.KEYCODE_BACK == keyCode) {
            long c = System.currentTimeMillis();
            if ((c - b) >= a) {
                ToastUtils.showToast("再按一次退出程序");
                b = c;// 把点击的时间归零
            } else {
                Intent mIntent = new Intent(ACTION_NAME);
                // 发送广播
                sendBroadcast(mIntent);

                ExitApplication.getInstance().AppExit(MainFragmentActivity.this);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销广播
        this.unregisterReceiver(mReceiver);
    }

    /**
     * 获取当前的网络状态 -1:没有网络 1:WIFI网络 2:wap网络 3:net网络
     *
     * @param context
     * @return int
     */
    public static int getAPNType(Context context) {
        connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null) {
            netType = -1;
            main_network.setVisibility(View.VISIBLE);
            // ToastUtils.showToast("当前无网络连接");
        } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
                netType = CMNET;
                main_network.setVisibility(View.GONE);
                //                ToastUtils.showToast("当前网络为：GPRS");
            } else {
                netType = CMWAP;
                main_network.setVisibility(View.GONE);
                // ToastUtils.showToast("当前网络为：GPRS");
            }
        } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = WIFI;
            main_network.setVisibility(View.GONE);
            // ToastUtils.showToast("当前网络为:WIFI");
        }
        return netType;
    }


}
